<?php


require('phpquery/phpQuery/phpQuery.php');


/*
 * 抓取企业微信的文档保存到目录中,并提交git
 */
class Parse 
{
	static $total = 0;
	public static function thislog($info) {
		$logInfo = date('Y-m-d H:i:s ') . $info . "\n";
		file_put_contents("info.log", $logInfo, FILE_APPEND);
		echo $logInfo;
	}

	public static function getJson($path) {
		global $total;
		$total ++;
		$fileName = "https://work.weixin.qq.com/api/doc" . $path;
		phpQuery::newDocumentFile($fileName);
		foreach(pq('script') as $key=>$one) {
			$content = pq($one)->html();
			if(strpos($content, "window.settings = ") != false) {
				$content = explode("\n", $content)[1];
				$content = rtrim($content, ";");
				$content = ltrim($content, "window.settings = ");
				$arr = json_decode( $content, true );
				return $arr;
			}
		}
        return null;
	}

	public static function parseDoc($path) {
		$arr = self::getJson($path);
		// file_put_contents("tmpFile.json", json_encode($arr['preDoc']['content_html'], JSON_PRETTY_PRINT | 320));
		$content = $arr['preDoc']['content_txt'];
        $content = str_replace("&quot;", '"',$content);
        self::thislog(self::$total . ":" . $path . ",length:" . strlen($content));
		return $content;
	}

	public static function asyncDir($map, $pre, $one, $dept=0) {
        if($dept>3) {
            return;
        }
		$one['title'] = str_replace("/", "-", $one['title']);
		$dirName = $pre . "/" . $one['title'] . "." . $one['category_id'];
        self::thislog($dirName);
		if(isset($one['children'])) {
			if(!is_dir($dirName)){
				mkdir($dirName);
                $dirNameShort = str_replace("data/", "data2/",$dirName);
                $dirNameShort = preg_replace("/.[0-9]{5}/", '', $dirNameShort);
                mkdir($dirNameShort);
			}
			if($one['children']) {
				foreach($one['children'] as $two) {
                    self::asyncDir($map, $dirName, $two, $dept+1);
				}
			}
		} else {
			$fileName = $dirName. ".txt";
			$path = preg_replace("/[^0-9\/]+/", "", $fileName);
			$path = preg_replace("/\s]+/", "", $path);
			$arr = explode("/", $path);
			if(count($arr) >= 5) {
				$path = "/" . $arr[1] . "/" . $arr[2] . "/" . end($arr);
			}
			$content = self::parseDoc($path);
			if( $content != "" && (!is_file($fileName) || file_get_contents($fileName) != $content )) {
				file_put_contents($fileName, $content);
                $fileNameShort = str_replace("data/", "data2/", $fileName);
                $fileNameShort = preg_replace("/.[0-9]{5}/", '', $fileNameShort);
				file_put_contents($fileNameShort, $content);
			} else {
                self::thislog("no update:". $path);
			}
		}
	}

	public static function parsePath() {
        self::thislog("start to deal.");
		// 获取定级目录
		$arr = json_decode(file_get_contents("other/struct.json"), true);
		// catagoriesList 处理分类
		// catagoriesMap 分类信息
		// preDoc 展示文档
		$pre = "data";
		foreach($arr['catagoriesList'] as $one) {
			self::asyncDir($arr['catagoriesMap'], $pre, $arr['catagoriesMap'][$one['id']]);
		}
        self::thislog("finish to deal.");
	}
}
